﻿using CBS.APP;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddStudentCost.xaml 的交互逻辑
    /// </summary>
    public partial class AddStudentCost : Window
    {
        public AddStudentCost()
        {
            InitializeComponent();

            LoadCondation();
        }

        private async void LoadCondation()
        {
            // 加载 ComboBox 数据
            await LoadDataAsync();

            //绑定数据
            int studentId = ((StudentSelectModel)cb_Student.SelectedItem).StudentId;
            tb_Account.Text = StudentAmountApp.GetForm(studentId).Balance.ToString();

            // 查询 Grid 数据
            //FlushGrid();
        }

        private async Task LoadDataAsync()
        {
            // 使用 Task.WhenAll 等待所有 ComboBox 数据加载完成
            var tasks = new List<Task>
            {
                BindSemester(),
                BindStudent(),
            };

            await Task.WhenAll(tasks);
        }

        private async Task BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0) return;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                cb_Semester.SelectedIndex = 0;
                //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async Task BindStudent()
        {
            try
            {
                var list = await Task.Run(() => StudentApp.GetSelectList());
                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Student.ItemsSource = list;
                }));
                cb_Student.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            int studentId = ((StudentSelectModel)cb_Student.SelectedItem).StudentId;

            string refundStr = tb_AmountRefund.Text.Trim();
            bool parseResult = decimal.TryParse(refundStr, out decimal account);
            if (!string.IsNullOrWhiteSpace(refundStr) && !parseResult)
            {
                MessageBox.Show(this, "退款金额填写格式错误！", "填写错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            string msg = string.Empty;
            if (StudentAmountApp.StudentAccountRefund(semesterId, studentId, account))
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show(this, msg, "操作失败", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }
    }
}
